<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:category:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:category:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:category:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:category:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="分类名称" align="center" prop="name" />
       <el-table-column
        label="跳转类型"
        align="center"
        prop="xcxUrl"
        :formatter="h5StatusFormat"
      />
       <el-table-column label="跳转地址" align="center" prop="xcxUrl" />
      <el-table-column label="分类icon" align="center" :show-overflow-tooltip="true" prop="url" />
<!--      <el-table-column label="是否删除" align="center" prop="isDelete" :formatter="isDeleteFormat" />-->
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:category:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:category:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-show="false" label="分类名称" prop="name">
          <el-input v-model="form.id" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类icon" prop="url">
         <!-- <el-input v-model="form.url" placeholder="请输入分类icon" /> -->
          <el-upload
            :headers="videoUpload.headers"
            :action="videoUpload.url"
            :file-list="poster"
            list-type="picture-card"
            :on-success="handlePagePhotoSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handlePagePhotoRemove"
            accept=".jpg,.jpeg,.png,,.JPG,.JPEG"
            :limit="1">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
          <el-form-item label="是否是H5">
          <el-radio-group v-model="form.isH5">
            <el-radio
              v-for="dict in h5StatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
          <div v-if="form.url!=null"> <el-form-item label="icon地址" prop="url">
        </el-form-item></div>
        <div v-if="form.isH5==1"> <el-form-item label="H5地址" prop="xcxUrl">
          <el-input v-model="form.xcxUrl" placeholder="H5地址" />
        </el-form-item></div>
          <!-- <div  v-if="form.isH5==0">
               <el-form-item label="小程序路由" prop="xcxUrl">
          <el-input v-model="form.xcxUrl" placeholder="小程序路由" />
        </el-form-item>
          </div> -->
        <el-form-item label="顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="顺序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCategory, getCategory, delCategory, addCategory, updateCategory, exportCategory } from "@/api/content/category";
  import { getToken } from '@/utils/auth'
  export default {
    name: "Category",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 分类表格数据
        categoryList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否删除字典
        isDeleteOptions: [],
            h5StatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          url: undefined,
          isDelete: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name :[{required: true, message: '必填项目', trigger: 'blur'}],
          orderNum :[{required: true, message: '必填项目', trigger: 'blur'}],
        },
        videoUpload:{
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/sys/file/upload",
          url2: process.env.VUE_APP_BASE_API,
        },
        fileList:[],
        file:"",
        poster:[],
        t1:[]
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_delete_status").then(response => {
        this.isDeleteOptions = response.data;
       
      });
          this.getDicts("plat_is_h5").then(response => {
      this.h5StatusOptions = response.data;
    });
    },
    methods: {
      /** 查询分类列表 */
      getList() {
        this.loading = true;
        listCategory(this.queryParams).then(response => {
          this.categoryList = response.rows;
          this.total = response.total;
          this.loading = false;
         
        });
      },
      // 是否删除字典翻译
      isDeleteFormat(row, column) {
        return this.selectDictLabel(this.isDeleteOptions, row.isDelete);
      },
        h5StatusFormat(row, column) {
      return this.selectDictLabel(this.h5StatusOptions, row.isH5);
    },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          url: undefined,
          createTime: undefined,
          isDelete: undefined,
          orderNum:undefined,
          poster:undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.poster =[];
        this.form.id ="add";
        this.open = true;
        this.title = "添加分类";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getCategory(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改分类";
           var  vm= new Array();
          vm.push(response.data.url)
          this.poster = vm.map(item => {
            return {
              url: item
            }
          });
           console.log("修改"+response)
            this.t1 = [];
           this.t1.push(response.data.url)
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        console.log(this.form)
        this.$refs["form"].validate(valid => {
          console.log("文件")
         console.log()
          if(this.t1.length==0){
              this.msgError("请选择图片上传");
            return
          }
          console.log(this.t1)
           this.form.url = this.t1[0]
          if (valid) {
            console.log("id"+this.form.id)
            if (this.form.id != "add") {
              updateCategory(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              this.form.id = null;
              addCategory(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除分类编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCategory(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有分类数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCategory(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
      handlePagePhotoRemove(file, fileList) {
        console.log("移除")
        console.log(fileList)
        console.log(file);
        this.poster = fileList;
        this.t1 =[];
        for(var i =0;i<fileList.length;i++){
          this.t1.push(fileList[i].url)
        }
      },

      handlePagePhotoSuccess(res, file){
        console.log(res)
        this.poster.push(file)
        this.t1.push(file.response.url)
      },
      handlePictureCardPreview(file) {
        console.log("预览")
      },
      getUrl(v){
        if (v.length==0){
          return null;
        }
        var  arr = new Array();
        for (var i =0;i<v.length;i++){
          if (v[i].response==null||v[i].response==undefined){
            arr.push(v[i].url)
          }else {
            arr.push(v[i].response)
          }
        }
        return arr;
      },

    },


  };
</script>
